import React, { Component } from 'react'
import {ListBox} from '../../../components'
import UpdateRolePage from './Update'
import AssignRoleRes from './AssignRoleRes'
import AssignFactoryRes from './AssignFactoryRes'
import { Modal } from 'antd';
import { fetchData } from '../../../components/AjaxConsumer';
export default class ListRolePage extends Component {
    constructor(props) {
        super(props);
        //行操作列表
        this.rowOperation = [

        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'add',
                showName: '新增',
                iconClass: 'plus'
            },{
                handleName: 'edit',
                showName: '修改',
                iconClass: 'edit'
            },{
                handleName: 'delete',
                showName: '删除',
                iconClass: 'delete'
            },{
                handleName: 'assignResource',
                showName: '分配资源',
                iconClass: 'key'
            }
            // ,{
            //     handleName: 'assignFactory',
            //     showName: '分配工厂',
            //     iconClass: 'key'
            // }
        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'ListRoleColumnConfig',
            data:[
                {
                    showName: 'id',
                    columnHidden: true,
                    columnName: 'resroleId',
                    foreverHidden: true
                },{
                    showName: '角色编码',
                    columnName: 'resroleCode',
                    columnWidth: 130
                },{
                    showName: '角色名称',
                    columnName: 'resroleName'
                },{
                    showName: '角色类型',
                    columnName: 'resreoleType',
                    showOption: []   
                },{
                    showName: '创建人',
                    columnName: 'createBy'
                },{
                    showName: '创建时间',
                    columnName: 'gmtCreate'
                },{
                    showName: '更新人',
                    columnName: 'lastModifiedBy'
                },{
                    showName: '更新时间',
                    columnName: 'gmtModified'
                },{
                    showName: '备注',
                    columnName: 'remark',
                    columnWidth: 130
                }
            ]
        };
        //搜索属性配置
        this.searchComponentData = [
            {
                showName: '角色编码',
                name:'resroleCode',
                type: 'input',
                url: null
            },{
                showName: '角色名称',
                name:'resroleName',
                type: 'input',
                url: null
            },{
                name : 'resreoleType',
                type : 'select',
                optionList:[]
            },{
                showName: '创建人',
                name: 'createBy',
                type: 'input'
            },{
                name:'gmtCreate',
                type: 'date',
                maxName:'gmtCreateMax',
                minName:'gmtCreateMin',
                showTime: 1
            },{
                name:'gmtModified',
                type: 'date',
                maxName:'gmtModifiedMax',
                minName:'gmtModifiedMin',
                showTime: 1
            },{
                showName: '备注',
                name:'remark',
                type: 'input',
                url: null
            }
        ];
    }

    componentWillMount() {
        const options={
            url: '/base/dic/get/resreole_type',
            type: 'get'
        }
        fetchData(this, options, this.dicInitCallBackHandle);
    }

    dicInitCallBackHandle = (resData) =>{
        let optionList = [];
        optionList.push('');
        resData.data.map((e,index)=>{
            let tmp = {};
            tmp.optionKey = e.dicCode;
            tmp.optionShow = e.dicName;
            optionList.push(tmp);
        })
        this.tableColumn.data[3].showOption = optionList;
        this.searchComponentData[2].optionList = optionList;
    }

    rowHandles = (strHandles, rowObj) => {
        console.log("strHandles===ListResource=======", strHandles);
        if(strHandles === 'edit'){
            this.editHandle(rowObj);
        }else if(strHandles === 'delete'){
            this.deleteHandle(rowObj);
        }else if(strHandles === 'add'){
            this.addHandle();
        }else if(strHandles === 'assignResource'){
            this.assignRoleResHandle(rowObj);
        }else if(strHandles === 'assignFactory'){
            this.assignFactoryHandle(rowObj);
        }
    }

    addHandle = () => {
        console.log("====执行了新增用户操作====");
        this.refs.updateRole.getFormData[1].disable = false;
        this.refs.updateRole.addHandle();
    }
    editHandle = (rowObj) => {
        console.log("====执行了修改操作====");
        // 只能选择一条限制
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {
            this.refs.updateRole.getFormData[1].disable = true;
            this.refs.updateRole.editHandle(rowObj[0].data)
        }

    }

    deleteHandle = (rowObj) => {
        console.log("=======执行了删除操作====");
        // 只能选择一条限制
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {
            this.refs.updateRole.deleteHandle(rowObj[0].data)
        }

    }

    assignRoleResHandle = (rowObj) => {
        console.log("====执行了分配资源操作====");
        // 只能选择一条限制
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            //待分配角色
            let roleId = rowObj[0].data.resroleId;
            //待分配角色拥有的资源id数据
            //const ownResIds = ['2'];
            this.refs.assignResource.initModal(roleId);
        }
    }

    assignFactoryHandle = (rowObj) => {
        console.log("====执行了分配资源操作====");
        // 只能选择一条限制
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            //待分配角色
            let roleId = rowObj[0].data.resroleId;
            //待分配角色拥有的资源id数据
            //const ownResIds = ['2'];
            let resreoleType = rowObj[0].data.resreoleType;
            if(resreoleType == '0'){
                    Modal.warning({
                        title: '通用角色不能分配工厂',
                    });
            }else{
                this.refs.assignFactory.initModal(roleId);
            }
        }
    }

    //刷新列表
    refreshHandle = () => {
        this.refs.listRole.refreshHandle();
    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，test commit
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return(
            <div>
                <ListBox
                    ref = 'listRole'
                    title="角色管理"
                    isHideLeft={isHideLeft}
                    tableColumnData = {this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url = '/base/resrole/list'
                    rowHandles = {this.rowHandles}
                    rowOperation = {this.rowOperation}
                    headerOperation = {this.headerOperation}
                    isRowSingle = "true"
                />

                <UpdateRolePage
                        ref="updateRole"
                        refreshHandle = {this.refreshHandle}
                />

                <AssignRoleRes
                    ref="assignResource"
                    refreshHandle = {this.refreshHandle}
                />
                <AssignFactoryRes
                    ref="assignFactory"
                    refreshHandle = {this.refreshHandle}
                />
            </div>
        )
    }
}
